package com.A21MobileSales;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.A21.Object.Global;
import com.A21.Object.MSTSettings;
import com.A21MobileSales.DataBase.DatabaseHelper;
import com.A21MobileSales.JsonServices.Download;

public class CustomerSOA extends Activity {

	DatabaseHelper ObjDBHelper;
	String Code, CName;
	Button btn_Home;
	ImageButton btn_back;
	TextView tv_CName;
	String strHTML = "";
	WebView wv;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.customer_soa);
		try {

			if (getIntent().hasExtra("Code")) {
				Code = getIntent().getStringExtra("Code");
			}
			if (getIntent().hasExtra("CName")) {
				CName = getIntent().getStringExtra("CName");
			}
			
			
			wv=(WebView)findViewById(R.id.webview1); 
			wv.getSettings().setBuiltInZoomControls(true);
			tv_CName = (TextView) findViewById(R.id.tv_CName);
			btn_Home = (Button) findViewById(R.id.btn_home);
			btn_Home.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent _intent = new Intent();
					_intent.setClass(CustomerSOA.this, HomeScreen.class);
					startActivity(_intent);
				}
			});

			btn_back = (ImageButton) findViewById(R.id.btn_back);
			btn_back.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Bundle b = new Bundle();
					Intent _intent = new Intent();
					_intent.putExtras(b);
					_intent.setClass(CustomerSOA.this, CustomerMaster.class);
					startActivity(_intent);
				}
			});
			tv_CName.setText(CName); 
			new SOASyncTask().execute();

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private class SOASyncTask extends AsyncTask<String, Void, Void> {

		private ProgressDialog dialog = new ProgressDialog(CustomerSOA.this);
		String StrMsg = null;

		protected void onPreExecute() {
			this.dialog.setMessage("Loading...");
			this.dialog.setCanceledOnTouchOutside(false);
			this.dialog.show();
		}

		// automatically done on worker thread (separate from UI thread)
		protected Void doInBackground(final String... a) {
			try {

				String strUserName = Global.getLoggedInUser().User;
				String strPassword = String
						.valueOf(Global.getLoggedInUser().strPassword);
				ObjDBHelper = new DatabaseHelper(CustomerSOA.this);
				MSTSettings setting = ObjDBHelper.GetSystemSettingsById("1");
				Download ObjDL = new Download(CustomerSOA.this, strUserName,
						strPassword);
				strHTML = ObjDL.GetSOA(setting.SOAtype, Code);
				StrMsg = "Success";
			} catch (Exception ex) {
				StrMsg = ex.getMessage();
			}
			return null;
		}

		// can use UI thread here
		protected void onPostExecute(final Void unused) {

			if (this.dialog.isShowing()) {
				this.dialog.dismiss();
				this.dialog = null;
			}
			try {
				if (StrMsg != null) {
					if (StrMsg.equals("Success")) {
						wv.loadData(strHTML, "text/html", "UTF-8");
					} else {
						Toast.makeText(CustomerSOA.this, StrMsg,
								Toast.LENGTH_SHORT).show();
					}
				} else {
					Toast.makeText(CustomerSOA.this,
							"Please check the network connection!!!",
							Toast.LENGTH_SHORT).show();
				}

			} catch (Exception ex) {

				ex.printStackTrace();

			}

		}

	}

}